package com.zhida.smartcontrol.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.dyhdyh.widget.loadingbar2.LoadingBar;
import com.dyhdyh.widget.loadingbar2.factory.LoadingFactory;
import com.zhida.smartcontrol.R;

public class CustomDialogFactory implements LoadingFactory<Context, Dialog> {
    private String msg="";
    public CustomDialogFactory(String msg) {
        this.msg=msg;
    }
    /**
     * 工厂类的标识符
     * 在cancel()之前多次调用show()时，当key相同时不会重新调用onCreate
     *
     * @return
     */
    @Override
    public String getKey() {
        return getClass().getName();
    }

    @Override
    public Dialog onCreate(Context context) {
        View view=LayoutInflater.from(context).inflate(R.layout.layout_loading,null);
        if (!msg.equals("")){
           TextView tv_msg= view.findViewById(R.id.tv_msg);
            tv_msg.setVisibility(View.VISIBLE);
            tv_msg.setText(msg);
        }
        AlertDialog dialog = new AlertDialog.Builder(context, R.style.NobackDialog)
                .setView(view)
                .setCancelable(false)
                .create();
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    LoadingBar.dialog(context).cancel();
                    return true;
                } else {
                    return false; //默认返回 false
                }
            }
        });
        return dialog;
    }

    @Override
    public void updateStatus(Object[] extras) {
        //每次调用show会回调这里  还有携带的参数
    }
}